package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.entity.User;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private UserService userService;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.判断当前菜品或套餐是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 获取当前用户ID
        Long userId = userService.getCurrentUserId(); // 确保此方法返回有效用户ID
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0) {
            //2.如果已经存在，数量加1
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //3.如果不存在，添加到购物车，数量默认为1
            // 判断当前添加的是菜品还是套餐
            // 如果是菜品，获取菜品的id
            // 如果是套餐，获取套餐的id
            Long dishId = shoppingCartDTO.getDishId();
            // 如果是菜品，获取菜品的口味
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 如果是套餐，获取套餐的菜品
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal byId = setmealMapper.getById(setmealId);
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
            }
            shoppingCart.setNumber(1);
            // 插入到购物车
            log.info("添加购物车:{}", shoppingCart);
            shoppingCartMapper.insert(shoppingCart);
        }


        //4.如果数量为0，从购物车中删除该菜品或套餐
        //5.如果数量不为0，更新购物车中菜品或套餐的数量
        log.info("添加购物车成功");
    }

    // 获取购物车数据
    @Override
    public List<ShoppingCart> list() {
        // 获取当前用户ID
        Long userId = userService.getCurrentUserId();
        // 构造查询条件
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        // 查询
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }


    // 清空购物车
    @Override
    public void clean() {
        Long userId = userService.getCurrentUserId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    // 减少购物车菜品数量
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        // 获取当前用户ID
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = userService.getCurrentUserId();
        shoppingCart.setUserId(userId);
        // 查询
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0) {
            // 如果存在，数量减1
            ShoppingCart cart = list.get(0);
            if (cart.getNumber() == 1) {
                // 如果数量为1，从购物车中删除该菜品或套餐
                shoppingCartMapper.deleteById(cart.getId());
            } else {
                // 如果数量不为1，更新购物车中菜品或套餐的数量
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(cart);
            }
        }
    }
}
